package models;

import java.util.List;

import javax.persistence.Entity;
import javax.persistence.ManyToOne;

import play.data.validation.Required;
import play.db.jpa.Model;

/**
 * UnregisteredInvitatiin entity creates an invitation that is sent to
 * UnregisteredEmail
 * 
 * @author Sara Assem khalil
 */
@Entity
public class UnregisteredInvitation extends Model {
	/**
	 * the organization id which the user is invited to
	 * 
	 * @author Sara Assem khalil
	 */
	@Required
	public Long organizationId;
	/**
	 * the invitation can only belong to one unregistered email
	 * 
	 * @author Sara Assem khalil
	 */

	@ManyToOne
	public UnRegisteredEmail email;

	/**
	 * the message of the invitation
	 * 
	 * @author Sara Assem khalil
	 */
	@Required
	public String message;
	/**
	 * the type of the invitation whether being an organizer or idea developer
	 * 
	 * @author Sara Assem khalil
	 */

	public String type;

	/**
	 * the constructor of the UnregisteredInvitation which takes as parameters
	 * 
	 * @param message
	 * @param email
	 * @param type
	 * @param organizationId
	 * @author Sara Assem khalil
	 * 
	 */
	public UnregisteredInvitation(String message, UnRegisteredEmail email,
			String type, Long organizationId) {

		this.message = message;
		this.email = email;
		this.type = type;
		this.organizationId = organizationId;

	}

	/**
	 * checks whether this invitation was sent before or not
	 * 
	 * @param unemail
	 * @param user
	 * @param type
	 * @param id
	 * @return boolean whether it is duplicate or not
	 * 
	 * @author Sara Assem
	 */
	public static boolean compareInvitation(String unemail, User user,
			String type, long id) {
		List<UnregisteredInvitation> invitations = UnregisteredInvitation
				.findAll();
		System.out.println(unemail);
		System.out.println(type);
		System.out.println(id);
		boolean checkmail = true;
		boolean checkid = true;
		boolean checktype = true;
		boolean checkall = true;
		for (int i = 0; i < invitations.size(); i++) {
			UnregisteredInvitation invitation = invitations.get(i);
			if (invitation.email.email.equalsIgnoreCase(unemail)) {
				checkmail = false;
				System.out.println(checkmail);

			}
		}

		for (int i = 0; i < invitations.size(); i++) {
			UnregisteredInvitation invitation = invitations.get(i);
			if (invitation.type.equalsIgnoreCase(type)) {
				checktype = false;
				System.out.println(checktype);
			}
		}
		for (int i = 0; i < invitations.size(); i++) {
			UnregisteredInvitation invitation = invitations.get(i);
			if (invitation.id.equals(id)) {
				checkid = false;
				System.out.println(checkid);
			}
		}
		if (checkmail != false)
	{
			checkall = false;
		}
		if( checktype != false){
			checkall = false;
			
			
		}
		if( checkid != false)
		{
			checkall = false;
				
		}
		return checkall;

	}

}
